import agent
import random
import time
import POS
import nltk
import math
from agent import Feedback

class VowelAgent(agent.Agent):
    
    def __init__(self, name):
        self.tulkki=POS.pos();
        self.tulkki.__init__();
        self.vowelWeight = random.random();
        self.consonantWeight = random.random();
        self.generateVowel = 0.5;
        agent.Agent.__init__(self, name);
        self.seenFeedback=0;
        
    def lifeCycle(self):
        r = random.random();
        if r < 0.33:
            print("generating...")
            self.generate()
        if r > 0.33 and r < 0.66:
            unratedwords = self.getUnscoredWords()
            if len(unratedwords) > 0:
                score = self.score(unratedwords[0].word)
                wCount=1-abs(score[1]-12)/float(14)
                if wCount<0.3:
                    wCount=0.3;
                scr=score[0]*wCount;
                if(score[1]>20):
                    framing="The sentence length was too high."
                    self.sendFeedback(unratedwords[0].word_id, scr, framing, wordtext=unratedwords[0].word)
                    return;
                if(score[1]<5):
                    framing="The sentence length was too low."
                    self.sendFeedback(unratedwords[0].word_id, scr, framing, wordtext=unratedwords[0].word)
                    return;
                if(score[0]<0.6):
                    framing="The sentences unique word count was too low."
                    self.sendFeedback(unratedwords[0].word_id, scr, framing, wordtext=unratedwords[0].word)
                    return;
                framing = "the sentence is the right length and has enough unique words."
                self.sendFeedback(unratedwords[0].word_id, scr, framing, wordtext=unratedwords[0].word)
        elif r > 0.66:
            feedback = self.getAllFeedback()
            if(len(feedback)>10):
                self.adapt(feedback)
            
        #time.sleep(1)
        
    def score(self, word):
        scr=0;
        #Unique words in sentence(Usually pretty close to 1, just a minor factor in evaluation)
        uniq=float(self.callFunction("mikkoHUniqueWords", word))/self.callFunction("numberOfWords", word);
        #Wordcount, Optimal length of input has been assigned to 12, under 2 and over 22 words get .3
        wCount=self.callFunction("numberOfWords", word);
        return (uniq,wCount);
    #Generates asks fro a sentence, if it does not pass my evaluation it is omitted.
    def generate(self):
        word=self.tulkki.generateSent()
        pros=self.score(word);
        wCount=1-abs(pros[1]-12)/float(14)
        if wCount<0.3:
            wCount=0.3;
        scr=pros[0]*wCount;
        if scr>0.5:
            explanation = "it scored:"+str(scr)+"in my evaluation."
            self.propose(word, explanation)

    def adapt(self, feedback):
        """
        Adapt gets all feedback from the server and updates the database according to feedback.
        """
        updated=0;
        maintNeed=0;
        for a in range(len(feedback)-10,len(feedback)):
            f=feedback.pop();
            #If score is high the agent takes that sentence into database or updates it's propability
            if f.score>0.80 and self.callFunction("numberOfWords", f.word)>4:
                updated+=self.tulkki.updateDB(f.word, 1);
            #If score is low the agent downgrades it's propability
            elif f.score<0.60 and self.callFunction("numberOfWords", f.word)>4:
                updated-=self.tulkki.updateDB(f.word, -1);
                maintNeed=1;
            #remove already seen feedback
        #removes low probability entries from database.
        if maintNeed==1:
            self.tulkki.maintenance();
        #Print how many additions and deletes were done
        print "I updated words and tagsets into my database:" + str(updated);
